package cn.ctyun.ipush.controller;


import cn.ctyun.ipush.service.ProdService;
import cn.ctyun.ipush.service.SaleService;
import cn.ctyun.ipush.utils.ConfigUtils;
import cn.ctyun.ipush.utils.MD5;
import cn.ctyun.ipush.web.user.bo.UserModel;
import cn.ctyun.ipush.web.user.service.UserAdminService;
import com.dexcoder.commons.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yanghongbo on 2017/5/10.
 */

@Controller
public class ManagerControl {

    @Autowired
    private SaleService saleService;

    @Resource
    private UserAdminService userAdminService;

    @Autowired
    private ProdService prodService;

    @RequestMapping(value = "salesGLYIntoPage", method = RequestMethod.GET)
    public ModelAndView salesGLYIntoPage(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的销售统计页面
         */
        try {
            Object o = session.getAttribute("userInfo");
            if (o != null) {
                String sql = "select a.*,b.prod_name,b.prod_type,d.city_name,c.location,c.user_realname from  hbbt_sales_his_model a inner join hbbt_product_model b on a.prod_id=b.hbbt_product_model_id inner join user_model c on a.create_userid=c.user_model_id inner join city d on d.city_id = c.city where a.is_deleted='0'";
                List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("hbbtSalesHisModelList", list);
                return new ModelAndView("IB_GLY_SEARCHBONUS", data);

            }
            return new ModelAndView("IB_GLY_SEARCHBONUS", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("IB_GLY_SEARCHBONUS", null);
        }
    }

    //glySearch
    @RequestMapping(value = "glySearch", method = RequestMethod.POST)
    public ModelAndView glySearch(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的销售统计页面
         */
        String bonusBeginTime = request.getParameter("bonusBeginTime");
        String bonusEndTime = request.getParameter("bonusEndTime");
        String searchProductType = request.getParameter("searchProductType");
        String searchProductId = request.getParameter("searchProductId");
        String searchRegion = request.getParameter("searchRegion");
        String searchSalesDepatment = request.getParameter("searchSalesDepatment");
        String searchSalesName = request.getParameter("searchSalesName");

        try {
            Object o = session.getAttribute("userInfo");
            if (o != null) {
//                String sql = String.format("select a.*,b.prod_name,b.prod_type,d.city_name,c.location,c.user_realname from " +
//                        " hbbt_sales_his_model a inner join hbbt_product_model b on a.prod_id=b.hbbt_product_model_id inner join user_model c on a.create_userid=c.user_model_id inner join city d on d.city_id = c.city " +
//                        "where a.is_deleted='0' and left(a.create_date,10) >= '%s' and left(a.create_date,10) <= '%s' and b.prod_type='%s' and b.prod_name='%s' and c.city='%s' and c.user_realname like '%%%s%%' and c.location ='%s'", bonusBeginTime, bonusEndTime, searchProductType, searchProductId, searchRegion, searchSalesName, searchSalesDepatment);

                String baseSql="select a.*,b.prod_name,b.prod_type,d.city_name,c.location,c.user_realname from " +
                        " hbbt_sales_his_model a inner join hbbt_product_model b on a.prod_id=b.hbbt_product_model_id inner join user_model c on a.create_userid=c.user_model_id inner join city d on d.city_id = c.city " +
                        "where a.is_deleted='0'";
                if(bonusBeginTime!=null&& !bonusBeginTime.equals("")){
                    baseSql += String.format(" and   left(a.create_date,10) >= '%s' ",bonusBeginTime);
                }
                if(bonusBeginTime!=null&& !bonusBeginTime.equals("")){
                    baseSql += String.format(" and   left(a.create_date,10) >= '%s' ",bonusBeginTime);
                }

                if(bonusEndTime!=null&& !bonusEndTime.equals("")){
                    baseSql += String.format(" and left(a.create_date,10) <= '%s' ",bonusEndTime);
                }

                if(searchProductType!=null&& !searchProductType.equals("")){
                    baseSql += String.format(" and b.prod_type='%s' ",searchProductType);
                }

                if(searchProductId!=null&& !searchProductId.equals("")){
                    baseSql += String.format(" and b.prod_name='%s'  ",searchProductId);
                }

                if(searchRegion!=null&& !searchRegion.equals("")){
                    baseSql += String.format(" and c.city='%s'   ",searchRegion);
                }

                if(searchSalesName!=null&& !searchSalesName.equals("")){
                    baseSql += String.format(" and c.user_realname like '%%%s%%'   ",searchSalesName);
                }

                if(searchSalesDepatment!=null&& !searchSalesDepatment.equals("")){
                    baseSql += String.format(" and c.location ='%s'  ",searchSalesDepatment);
                }

                List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), baseSql);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("hbbtSalesHisModelList", list);
                return new ModelAndView("IB_GLY_SEARCHBONUS", data);

            }
            return new ModelAndView("IB_GLY_SEARCHBONUS", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("IB_GLY_SEARCHBONUS", null);
        }
    }

    @RequestMapping(value = "userGLYIntoPage", method = RequestMethod.GET)
    public ModelAndView userGLYIntoPage(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的人员配置
         */

        try {
            Object o = session.getAttribute("userInfo");


            if (o != null) {
                UserModel ob = (UserModel) o;

                String sql = String.format("select a.* from  user_model a");
                List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("userModelList", list);
                return new ModelAndView("IB_GLY_USERMANAGE", data);


            }
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        }
    }

    @RequestMapping(value = "userGLYAddUser", method = RequestMethod.POST)
    public ModelAndView userGLYAddUser(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的人员配置
         */

        try {
            Object o = session.getAttribute("userInfo");


            if (o != null) {
                UserModel ob = new UserModel();
                String userLoginName = request.getParameter("userLoginName");
                String userRealName = request.getParameter("userRealName");
                String userTelePhone = request.getParameter("userTelePhone");
                String saleRegions = request.getParameter("saleRegions");
                String saleDepartment = request.getParameter("saleDepartment");
                String addRoles = request.getParameter("addRoles");
                ob.setUserLoginName(userLoginName);
                ob.setUserRealname(userRealName);
                ob.setUserPhone(userTelePhone);
                ob.setCity(saleRegions);
                ob.setLocation(saleDepartment);
                ob.setRole(addRoles);
                ob.setUserModelId(UUIDUtils.getUUID32());
                ob.setUserPassword(MD5.getMD5Code("1"));
                if (userAdminService.saveUser(ob)) {
                    UserModel userModel1 = new UserModel();

                    String sql = String.format("select a.* from  user_model a");
                    List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                    Map<String, Object> data = new HashMap<String, Object>();
                    data.put("userModelList", list);
                    return new ModelAndView("IB_GLY_USERMANAGE", data);
                }
                return new ModelAndView("IB_GLY_USERMANAGE", null);


            }
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        }
    }

    @RequestMapping(value = "userGLYSearchUser", method = RequestMethod.POST)
    public ModelAndView userGLYSearchUser(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的人员配置
         */

        try {
            Object o = session.getAttribute("userInfo");


            if (o != null) {
                String sql = String.format("select a.* from  user_model a where 1=1 ");
                UserModel ob = new UserModel();
                String userLoginName = request.getParameter("searchLoginName");
                String userRealName = request.getParameter("searchRealName");
                String userTelePhone = request.getParameter("searchTelePhone");
                String saleRegions = request.getParameter("searchSaleRegions");
                String saleDepartment = request.getParameter("searchSaleDepartment");
                String addRoles = request.getParameter("searchRoles");
                ob.setUserLoginName(userLoginName);
                ob.setUserRealname(userRealName);
                ob.setUserPhone(userTelePhone);
                ob.setCity(saleRegions);
                ob.setLocation(saleDepartment);
                ob.setRole(addRoles);

                if (userLoginName != null && !userLoginName.equals("")) {
                    sql = sql + " and user_login_name like '%" + userLoginName + "%'";
                }
                if (userRealName != null && !userRealName.equals("")) {
                    sql = sql + " and user_realname like '%" + userRealName + "%'";
                }
                if (userTelePhone != null && !userTelePhone.equals("")) {
                    sql = sql + " and user_phone = '" + userRealName + "'";
                }
                if (saleRegions != null && !saleRegions.equals("")) {
                    sql = sql + " and city = '" + saleRegions + "'";
                }
                if (saleDepartment != null && !saleDepartment.equals("")) {
                    sql = sql + " and location = '" + saleDepartment + "'";
                }
                if (addRoles != null && !addRoles.equals("-1")) {
                    sql = sql + " and role = '" + addRoles + "'";
                }
                List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("userModelList", list);
                return new ModelAndView("IB_GLY_USERMANAGE", data);
            }
            return new ModelAndView("IB_GLY_USERMANAGE", null);


        } catch (Exception e) {
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        }

    }


    @RequestMapping(value = "userGLYUpdateUser", method = RequestMethod.POST)
    public ModelAndView userGLYUpdateUser(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的人员配置
         */

        try {
            Object o = session.getAttribute("userInfo");



            if (o != null) {
                String sql = String.format("select a.* from  user_model a where 1=1 ");
                UserModel ob = new UserModel();
                String userLoginName=request.getParameter("alteruserLoginName");
                String userRealName=request.getParameter("alteruserRealName");
                String userTelePhone=request.getParameter("alteruserTelePhone");
                String saleRegions=request.getParameter("altersaleDepartment");
                String saleDepartment=request.getParameter("altertSaleDepartment");
                String addRoles=request.getParameter("alterRoles");
                String userId=request.getParameter("userId");
                String userPassword=request.getParameter("userPassword");

                ob.setUserLoginName(userLoginName);
                ob.setUserRealname(userRealName);
                ob.setUserPhone(userTelePhone);
                ob.setCity(saleRegions);
                ob.setLocation(saleDepartment);
                ob.setRole(addRoles);
                ob.setUserModelId(userId);
                ob.setUserPassword(MD5.getMD5Code(userPassword));

                if(userAdminService.updateUser(ob)) {


                    List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                    Map<String, Object> data = new HashMap<String, Object>();
                    data.put("userModelList", list);

                    return new ModelAndView("IB_GLY_USERMANAGE", data);
                }
                return new ModelAndView("IB_GLY_USERMANAGE", null);
            }
            return new ModelAndView("IB_GLY_USERMANAGE", null);


        }catch (Exception e) {
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        }

    }
    @RequestMapping(value = "userGLYUpdateUserPa", method = RequestMethod.POST)
    public ModelAndView userGLYUpdateUserPassword(HttpSession session, HttpServletRequest request) {
        /**
         * 用于跳转到管理员页面的人员配置
         */

        try {
            Object o = session.getAttribute("userInfo");



            if (o != null) {



                UserModel ob = new UserModel();
                String userLoginName=request.getParameter("alteruserLoginName");
                String userRealName=request.getParameter("alteruserRealName");
                String userTelePhone=request.getParameter("alteruserTelePhone");
                String saleRegions=request.getParameter("altersaleDepartment");
                String saleDepartment=request.getParameter("altertSaleDepartment");
                String addRoles=request.getParameter("alterRoles");
                String userId=request.getParameter("userId");
                ob.setUserLoginName(userLoginName);
                ob.setUserRealname(userRealName);
                ob.setUserPhone(userTelePhone);
                ob.setCity(saleRegions);
                ob.setLocation(saleDepartment);
                ob.setRole(addRoles);
                ob.setUserModelId(userId);

                ob=userAdminService.getUserById(userId);

                ob.setUserPassword(MD5.getMD5Code("1"));

                if(userAdminService.updateUser(ob)) {

                    String sql = String.format("select a.* from  user_model a where 1=1 ");
                    List list = saleService.queryListBySql(ConfigUtils.getStringValue("jdbcUrl"), ConfigUtils.getStringValue("username"), ConfigUtils.getStringValue("password"), sql);
                    Map<String, Object> data = new HashMap<String, Object>();
                    data.put("userModelList", list);

                    return new ModelAndView("IB_GLY_USERMANAGE", data);
                }
                return new ModelAndView("IB_GLY_USERMANAGE", null);
            }
            return new ModelAndView("IB_GLY_USERMANAGE", null);


        }catch (Exception e) {
            return new ModelAndView("IB_GLY_USERMANAGE", null);
        }

    }

}
